<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                  |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                   |
// +------------------------------------------------------------------------+
// | Author: liguohua@clcw.com.cn Blog:http://blog.sina.com.cn/saverneo     |
// +------------------------------------------------------------------------+
//
/**
 * 抬（砍）价管理
 */

namespace AuctionBack\Controller;

use AuctionCore\System\AuctionException;

class BargainController extends BaseController {

    public $emp_id = 0;

    public function __construct() {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
    }

    /**
     *
     */
    public function index() {
        $come_from = new \AuctionCore\Other\Channel(3);
        $from = $come_from->get_children_channel();
        foreach($from as $key=>$val){
            if($val['from_id']=='-1')unset($from[$key]);
            $from[$key]=$val;
        }
        $this->assign('come_from',$from);
        $this->display();
    }

    //车辆列表
    public function get_list() {
        $page = I("get.p", 1);

        $size = 10;
        $params = array();
        $params['status'] = I("request.status",0);

        $params['car_no'] = I("request.car_no");
        $params['brand_id'] = I("request.brand_id");
        $params['series_id'] = I("request.series_id");
        $params['model_id'] = I("request.model_id");

        $params['phone'] = I("request.phone");
        $params['come_from'] = I("request.come_from");

        //获取list
        $mycar = new \AuctionCore\Other\MyCar();
        $result = $mycar->get_cars_up($page, $size, $params);

        $Page = new \AuctionCore\Common\AjaxPage($result['count'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show(); // 分页显示输出

        //模板赋值
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->display();
    }

    /**
     * 活动详情
     */
    public function detail(){
            $car_id = I('request.car_id');

            $detail = array();

            //车源信息
            $car = new \AuctionCore\CarResource\Car($car_id);
            $car_detail = $car->detail();
            $detail['car_id'] = $car_detail['car_id'];
            $detail['car_no'] = $car_detail['car_no'];
            $detail['bid_up_price'] = $car_detail['bid_up_price'];
            //车型
            $cm = new \AuctionCore\CarResource\CarModel();
            $series = $cm->get_model_by_id($car_detail['series_id']);
            $detail['model_show'] = $series['name'] . ' ' . $car_detail['car_cc'] . ($car_detail['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $car_detail['gearbox']);

            $chanel = new \AuctionCore\Other\Channel(3);
            $arr = $chanel->get_children_channel();
            $come_from = array();
            foreach($arr as $key=>$val){
                $come_from[$val['from_id']]=$val['from_name'];
            }


            //车主信息
            $owner = new \AuctionCore\CarOwner\Owner($car_detail['owner_id']);
            $owner_detail=$owner->detail();
            $detail['phone'] = $owner_detail['phone'];
            $detail['comefrom'] = $come_from[$owner_detail['comefrom']];

            //抬价次数
            $my_car = new \AuctionCore\Other\MyCar();
            $map = array();
            $map['type'] =array('eq',1);//卖车
            $arr= $my_car->get_bargain_log(1,10,$map);
            $detail['count'] = $arr[1];

            $this->assign('detail',$detail);
            $this->display();
    }



    //获取抬价日志
    public function get_bargain_logs(){
        $page = I("request.p", 1);
        $type = I("request.type", 1);
        $car_id = I("request.car_id");
        $size = 10;

        //
        $mycar = new \AuctionCore\Other\MyCar();
        $map = array();
        $map['type'] = $type;
        $map['car_id'] = $car_id;

        $result =   $mycar->get_bargain_log($page,$size,$map);
        //
        $Page = new \AuctionCore\Common\AjaxPage($result[1], $size, "get_list");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show(); // 分页显示输出

        //模板赋值
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('logs', $result[0]);
        $this->display('log_list');


    }

    /**
     * 关闭或打开活动
     */
    public function bargain_switch(){
        $type = I('request.type',1);
        $car_id = I('request.car_id');
        if(empty($car_id)){
            $this->helpJsonResult(420, "车源ID不能为空");
        }

        $car = new \AuctionCore\CarResource\Car($car_id);
        $data = array();
        $data['is_game_over'] = $type==1?'1':'0';
        $rs = $car->update($data);

        $msg = $type == 1 ? "关闭" : "开启";
        if ($rs!==false) {
            $this->helpJsonResult(200, "活动".$msg."成功");
        } else {
            $this->helpJsonResult(508, "活动".$msg."失败");
        }
        exit;

    }
    /**
     * 设置费用
     */
    public function set_bargain() {

        //获取拍卖公共设置
        $auSetting = new \AuctionCore\AuctionOrder\AuctionSetting();
        $data = array();
        $data['value'] = I('post.commision');
        //设置交易佣金
        $cond = "name='COMMISION'";
        $res1 = $auSetting->update($cond, $data);
        //设置交易佣金
        $data = array();
        $data['value'] = I('post.transfer_fee');
        $cond = "name='TRANSFER_FEE'";
        $res2 = $auSetting->update($cond, $data);
        if ($res1 && $res2) {
            $this->helpJsonResult(200, "设置成功");
        } else {
            $this->helpJsonResult(508, "数据未更新");
        }
        exit;
    }

}
